package com.shop.shoppingmall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.shoppingmall.entity.ShProductCategory;
import com.shop.shoppingmall.service.ShProductCategoryService;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 商品分类表(ShProductCategory)表控制层
 *
 * @author makejava
 * @since 2020-09-07 15:49:59
 */
@RestController
@RequestMapping("shProductCategory")
@CrossOrigin
public class ShProductCategoryController {
    /**
     * 服务对象
     */
    @Resource
    private ShProductCategoryService shProductCategoryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ShProductCategory selectOne(String id) {
        return this.shProductCategoryService.queryById(id);
    }

    /**
     * 添加商品分类
     * @param jsonObject
     * @return
     */
    @PostMapping("insert")
    public Result insert(@RequestBody JSONObject jsonObject) {
        Result<JSONObject> result = new Result<JSONObject>();
        ShProductCategory shProductCategory = JSON.parseObject(jsonObject.toJSONString(), ShProductCategory.class);
        return shProductCategoryService.insert(shProductCategory);
    }
    /**
     * 查询所有分类及子分类
     * @return
     */
    @GetMapping("listAll")
    public Result listAll() {
        return shProductCategoryService.listAll();
    }
    /**
     * 查询所有父级分类
     * @return
     */
    @GetMapping("queryByParentId")
    public Result queryByParentId() {
        return shProductCategoryService.queryByParentId();
    }

    /**
     * 根据父级分类id查询所有子分类
     * @param parentId
     * @return
     */
    @GetMapping("selectParentId")
    public Result selectParentId(@RequestParam("parentId") String parentId) {
        return shProductCategoryService.selectParentId(parentId);
    }


    @PostMapping("deleteParentId")
    public Result deleteParentId(@RequestBody Map map) {
       String  categoryId =map.get("categoryId").toString();
        return shProductCategoryService.deleteParentId(categoryId);
    }
}